﻿//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.IO;
using System.Runtime.Serialization.Json;

namespace Microsoft.Ted.Wacel.Base
{
    public static class JsonSerializer
    {

        public static byte[] Serialize(Type type, object value)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(type);

            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, value);
                return ms.ToArray();
            }
        }
        public static object Deserialize(Type type, byte[] data)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(type);

            using (MemoryStream ms = new MemoryStream(data))
            {
                return serializer.ReadObject(ms);
            }
        }
    }
}
